package com.scau.XFourPOS.action;

import java.util.List;

import com.scau.XFourPOS.model.Customer;
import com.opensymphony.xwork2.ModelDriven;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.apache.log4j.Logger;  
import com.scau.XFourPOS.model.*;
import com.scau.XFourPOS.service.CustomerService;
import com.scau.XFourPOS.utils.PageUtils;

/**
 * 类名：CustomerAction 类描述：商品控制器 创建人：庄祥鹏
 * */
@Controller
public class CustomerAction extends BaseAction implements ModelDriven<Customer> {
	static Logger log = Logger.getLogger(CustomerAction.class);
	@Autowired
	private CustomerService customerService;
	public Customer customer = new Customer();

	/**
	 * 添加操作
	 */
	@Action("customerSaveAction")
	public void save() {
		Json result = new Json();
		try {
			customerService.saveEntity(customer);
			result.setSuccess(true);
			result.setMsg("添加用户成功");
		} catch (Exception e) {
			result.setMsg("添加用户失败");
		}
		writeJson(result);
	}
	/**
	 * 删除操作
	 */
	@Action("customerDeleteAction")
	public void delete() {
		Json result = new Json();
		try {
			customerService.deleteEntityById(id);
			result.setSuccess(true);
			result.setMsg("删除用户成功");
		} catch (Exception e) {
			result.setMsg("删除用户失败");
		}
		writeJson(result);
	}
	/**
	 * 得到数据列表
	 */
	@Action("customerGridAction")
	public void grid() {
		PageUtils<Customer> pageUtils = new PageUtils<Customer>(page, rows,
				sort, order);
		System.out.println("page"+page+";rows"+rows+"--------------------");
		List<Customer> list = customerService.loadAllByPage(pageUtils)
				.getResult();
		long total = pageUtils.getTotalCount();
		writeJson(total, list);
	}

	/**
	 * 修改操作
	 */
	@Action("customerUpdateAction")
	public void update() {
		Json result = new Json();
		try {
			customerService.updateEntity(customer);
			result.setSuccess(true);
			result.setMsg("修改用户成功");
		} catch (Exception e) {
			result.setMsg("添加用户失败");
		}
		writeJson(result);
	}

	@Override
	public Customer getModel() {
		return customer;
	}

}
